package com.wenresearch.orion.demoapp.screen;

import com.wenresearch.orion.demoapp.screen.accordion.AccordionScreen;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

/**
 * Default screen, list of demo showcase
 * 
 * @author Ali Irawan (boylevantz@gmail.com)
 * @version 1.0
 */
public class DefaultScreen extends MainScreen {

	private ButtonField _btnAccordion;
	private ButtonField _btnTab;
	private ButtonField _btnImageLoader;

	private ShowCaseHandler _handler;

	public DefaultScreen() {
		setTitle("Orion UI for BlackBerry");

		VerticalFieldManager main = new VerticalFieldManager(Field.USE_ALL_WIDTH);

		getMainManager().add(main);
		
		main.add(new LabelField("Welcome to Orion UI for BlackBerry! "));
		main.add(new LabelField("Pick from menu below for showcase"));

		
		// Create buttons
		_btnAccordion = new ButtonField("Accordion", ButtonField.CONSUME_CLICK | Field.FOCUSABLE | Field.USE_ALL_WIDTH);
		_btnTab = new ButtonField("Tab", ButtonField.CONSUME_CLICK | Field.FOCUSABLE | Field.USE_ALL_WIDTH);
		_btnImageLoader = new ButtonField("Image Loader", ButtonField.CONSUME_CLICK | Field.FOCUSABLE | Field.USE_ALL_WIDTH);

		_handler = new ShowCaseHandler();

		_btnAccordion.setChangeListener(_handler);
		_btnTab.setChangeListener(_handler);
		_btnImageLoader.setChangeListener(_handler);

		main.add(_btnAccordion);
		main.add(_btnTab);
		main.add(_btnImageLoader);
	}

	private class ShowCaseHandler implements FieldChangeListener {

		public void fieldChanged(Field field, int context) {
			if (field == _btnAccordion) {
				getApp().pushScreen(new AccordionScreen());
			} else if (field == _btnTab) {
				// TODO
				
			} else if (field == _btnImageLoader) {
				// TODO
				
			}
		}

	}

	
	private UiApplication getApp(){
		return UiApplication.getUiApplication();
	}


	/* (non-Javadoc)
	 * @see net.rim.device.api.ui.container.MainScreen#onSavePrompt()
	 */
	protected boolean onSavePrompt() {
		
		return true;
	}


	/* (non-Javadoc)
	 * @see net.rim.device.api.ui.Screen#onClose()
	 */
	public boolean onClose() {
		// Ask in dialog
		int answer = Dialog.ask(Dialog.D_YES_NO, "Are you sure want to quit");
		if(answer==Dialog.YES){
			return super.onClose();
		}
		return false;
	}
	
	
}
